<?php 
include_once(dirname(__FILE__) . "/lib/defines.php");
include_once(dirname(__FILE__) . "/lib/Class.Table.php");



getpost_ifset(array('current_page', 'fromstatsday_sday', 'fromstatsmonth_sday', 'days_compare', 'min_call', 'posted',  'dsttype', 'sourcetype', 'clidtype', 'channel', 'resulttype', 'stitle', 'atmenu', 'current_page', 'order', 'sens', 'dst', 'src', 'clid', 'userfieldtype', 'userfield', 'accountcodetype', 'accountcode'));


if (!isset ($current_page) || ($current_page == "")){	
		$current_page=0; 
	}

// this variable specifie the debug type (0 => nothing, 1 => sql result, 2 => boucle checking, 3 other value checking)
$FG_DEBUG = 0;

// The variable FG_TABLE_NAME define the table name to use
$FG_TABLE_NAME=DB_TABLENAME;



// THIS VARIABLE DEFINE THE COLOR OF THE HEAD TABLE
$FG_TABLE_HEAD_COLOR = "#D1D9E7";


$FG_TABLE_EXTERN_COLOR = "#7F99CC"; //#CC0033 (Rouge)
$FG_TABLE_INTERN_COLOR = "#EDF3FF"; //#FFEAFF (Rose)




// THIS VARIABLE DEFINE THE COLOR OF THE HEAD TABLE
$FG_TABLE_ALTERNATE_ROW_COLOR[] = "#FFFFFF";
$FG_TABLE_ALTERNATE_ROW_COLOR[] = "#F2F8FF";



//$link = DbConnect();
$DBHandle  = DbConnect();

// The variable Var_col would define the col that we want show in your table
// First Name of the column in the html page, second name of the field
$FG_TABLE_COL = array();


/*******
Calldate Clid Src Dst Dcontext Channel Dstchannel Lastapp Lastdata Duration Billsec Disposition Amaflags Accountcode Uniqueid Serverid
*******/

$FG_TABLE_COL[]=array ("Дата звонка", "calldate", "18%", "left", "SORT", "19");
//$FG_TABLE_COL[]=array ("Channel", "channel", "8%", "left", "", "30", "", "", "", "", "", "display_acronym");
$FG_TABLE_COL[]=array ("Источник", "src", "10%", "center", "", "30");
$FG_TABLE_COL[]=array ("CallerID", "clid", "12%", "center", "", "30");
//$FG_TABLE_COL[]=array ("Lastapp", "lastapp", "0%", "center", "", "30");

//$FG_TABLE_COL[]=array ("Lastdata", "lastdata", "0%", "center", "", "30");
$FG_TABLE_COL[]=array ("Направление", "dst", "18%", "right", "SORT", "30");
//$FG_TABLE_COL[]=array ("APP", "dst", "15%", "center", "", "30","list", $appli_list);
//$FG_TABLE_COL[]=array ("Serverid", "serverid", "7%", "center", "", "30");
$FG_TABLE_COL[]=array ("Status", "disposition", "13%", "center", "", "30");
if ((!isset($resulttype)) || ($resulttype=="min")) $minute_function= "display_minute";
//$FG_TABLE_COL[]=array ("Duration", "duration", "6%", "center", "SORT", "30", "", "", "", "", "", "$minute_function");
$FG_TABLE_COL[]=array ("Duration", "billsec", "6%", "center", "SORT", "30", "", "", "", "", "", "$minute_function");

$FG_TABLE_COL[]=array ("Department-CallerID", "userfield", "14%", "center", "", "20");
$FG_TABLE_COL[]=array ("Accountcode", "accountcode", "10%", "center", "", "20");
$FG_TABLE_COL[]=array ("PDD", "pdd", "10%", "center", "", "20");

$FG_TABLE_DEFAULT_ORDER = "calldate";
$FG_TABLE_DEFAULT_SENS = "DESC";

// This Variable store the argument for the SQL query
//$FG_COL_QUERY='calldate, channel, src, clid, lastapp, lastdata, dst, dst, serverid, disposition, duration';
$FG_COL_QUERY="calldate, src, clid, dst, disposition, billsec, userfield, accountcode";
$FG_COL_QUERY_GRAPH="calldate, billsec, duration, IF(disposition = 'ANSWERED', (duration-billsec),NULL)";


// The variable LIMITE_DISPLAY define the limit of record to display by page
$FG_LIMITE_DISPLAY=25;

// Number of column in the html table
$FG_NB_TABLE_COL=count($FG_TABLE_COL);

// The variable $FG_EDITION define if you want process to the edition of the database record
$FG_EDITION=true;

//This variable will store the total number of column
$FG_TOTAL_TABLE_COL = $FG_NB_TABLE_COL;
if ($FG_DELETION || $FG_EDITION) $FG_TOTAL_TABLE_COL++;

//This variable define the Title of the HTML table
$FG_HTML_TABLE_TITLE=" - Call Logs - ";

//This variable define the width of the HTML table
$FG_HTML_TABLE_WIDTH="90%";




if ($FG_DEBUG == 3) echo "<br>Table : $FG_TABLE_NAME  	- 	Col_query : $FG_COL_QUERY";
$instance_table = new Table($FG_TABLE_NAME, $FG_COL_QUERY);
$instance_table_graph = new Table($FG_TABLE_NAME, $FG_COL_QUERY_GRAPH);


if ( is_null ($order) || is_null($sens) ){
	$order = $FG_TABLE_DEFAULT_ORDER;
	$sens  = $FG_TABLE_DEFAULT_SENS;
}


if ($posted==1){
	
  function do_field($sql,$fld){
  		$fldtype = $fld.'type';
		global $$fld;
		global $$fldtype;
        if (isset($$fld) && ($$fld!='')){
                if (strpos($sql,'WHERE') > 0){
                        $sql = "$sql AND ";
                }else{
                        $sql = "$sql WHERE ";
                }
				$sql = "$sql $fld";
				if (isset ($$fldtype)){                
                        switch ($$fldtype) {
							case 1:	$sql = "$sql='".$$fld."'";  break;
							case 2: $sql = "$sql LIKE '".$$fld."%'";  break;
							case 3: $sql = "$sql LIKE '%".$$fld."%'";  break;
							case 4: $sql = "$sql LIKE '%".$$fld."'";
						}
                }else{ $sql = "$sql LIKE '%".$$fld."%'"; }
		}
        return $sql;
  }  
  $SQLcmd = '';

  if ($_POST['before']) {
    if (strpos($SQLcmd, 'WHERE') > 0) { 	$SQLcmd = "$SQLcmd AND ";
    }else{     								$SQLcmd = "$SQLcmd WHERE "; }
    $SQLcmd = "$SQLcmd calldate<'".$_POST['before']."'";
  }
  if ($_POST['after']) {    if (strpos($SQLcmd, 'WHERE') > 0) {      $SQLcmd = "$SQLcmd AND ";
  } else {      $SQLcmd = "$SQLcmd WHERE ";    }
    $SQLcmd = "$SQLcmd calldate>'".$_POST['after']."'";
  }
  $SQLcmd = do_field($SQLcmd, 'clid');
  $SQLcmd = do_field($SQLcmd, 'src');
  $SQLcmd = do_field($SQLcmd, 'dst');
  $SQLcmd = do_field($SQLcmd, 'channel');  
    
  $SQLcmd = do_field($SQLcmd, 'userfield');
  $SQLcmd = do_field($SQLcmd, 'accountcode');
  
  
}


$date_clause='';
// Period (Month-Day)


if (!isset($fromstatsday_sday)){	
	$fromstatsday_sday = date("d");
	$fromstatsmonth_sday = date("Y-m");	
}



//if (isset($fromstatsday_sday) && isset($fromstatsmonth_sday)) $date_clause.=" AND calldate <= '$fromstatsmonth_sday-$fromstatsday_sday+23' AND calldate >= SUBDATE('$fromstatsmonth_sday-$fromstatsday_sday',INTERVAL $days_compare DAY)";

if (DB_TYPE == "postgres"){	
	if (isset($fromstatsday_sday) && isset($fromstatsmonth_sday)) $date_clause.=" AND calldate < date'$fromstatsmonth_sday-$fromstatsday_sday'+ INTERVAL '1 DAY' AND calldate >= date'$fromstatsmonth_sday-$fromstatsday_sday'";
}else{
	if (isset($fromstatsday_sday) && isset($fromstatsmonth_sday)) $date_clause.=" AND calldate < ADDDATE('$fromstatsmonth_sday-$fromstatsday_sday',INTERVAL 1 DAY) AND calldate >= '$fromstatsmonth_sday-$fromstatsday_sday'";  
}

if ($FG_DEBUG == 3) echo "<br>$date_clause<br>";


  
if (strpos($SQLcmd, 'WHERE') > 0) { 
	$FG_TABLE_CLAUSE = substr($SQLcmd,6).$date_clause; 
}elseif (strpos($date_clause, 'AND') > 0){
	$FG_TABLE_CLAUSE = substr($date_clause,5); 
}

if ($_POST['posted']==1){
	//> function Get_list ($clause=null, $order=null, $sens=null, $field_order_letter=null, $letters = null, $limite=null, $current_record = NULL)
	$list = $instance_table -> Get_list ($FG_TABLE_CLAUSE, $order, $sens, null, null, $FG_LIMITE_DISPLAY, $current_page*$FG_LIMITE_DISPLAY);
	
	$list_total = $instance_table_graph -> Get_list ($FG_TABLE_CLAUSE, null, null, null, null, null, null);
}


if ($FG_DEBUG == 3) echo "<br>Clause : $FG_TABLE_CLAUSE";
//$nb_record = $instance_table -> Table_count ($FG_TABLE_CLAUSE);
$nb_record = count($list_total);
if ($FG_DEBUG >= 1) var_dump ($list);



if ($nb_record<=$FG_LIMITE_DISPLAY){ 
	$nb_record_max=1;
}else{ 
	$nb_record_max=(intval($nb_record/$FG_LIMITE_DISPLAY)+1);
}

if ($FG_DEBUG == 3) echo "<br>Nb_record : $nb_record";
if ($FG_DEBUG == 3) echo "<br>Nb_record_max : $nb_record_max";

?>

<script language="JavaScript" type="text/JavaScript">
<!--
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

//-->
</script>



<!-- ** ** ** ** ** Part for the research ** ** ** ** ** -->
	<center>
	<FORM METHOD=POST ACTION="<?php  echo $PHP_SELF?>?s=<?php  echo $s?>&t=<?php  echo $t?>&order=<?php  echo $order?>&sens=<?php  echo $sens?>&current_page=<?php  echo $current_page?>">
	<INPUT TYPE="hidden" NAME="posted" value=1>
		<table class="bar-status" width="75%" border="0" cellspacing="1" cellpadding="2" align="center">
			<tbody>
			
			<tr>
        		<td align="left" bgcolor="#000033">					
					<font face="verdana" size="1" color="#ffffff"><b>&nbsp;&nbsp;День</b></font>
				</td>
      			<td align="left" bgcolor="#acbdee">
					<table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#acbdee"><tr><td>
	  				<b>От : </b><select name="fromstatsday_sday" class="intxt">
					<?php  
						for ($i=1;$i<=31;$i++){
							if ($fromstatsday_sday==sprintf("%02d",$i)){$selected="selected";}else{$selected="";}
							echo '<option value="'.sprintf("%02d",$i)."\"$selected>".sprintf("%02d",$i).'</option>';
						}
					?>					
					</select>
				 	<select name="fromstatsmonth_sday" class="intxt">
					<?php 	$year_actual = date("Y");  	
						for ($i=$year_actual;$i >= $year_actual-1;$i--)
						{		   
							   $monthname = array( "Январь", "Февраль","Март", "Апрель", "Май", "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь");
							   if ($year_actual==$i){
									$monthnumber = date("n")-1; // Month number without lead 0.
							   }else{
									$monthnumber=11;
							   }		   
							   for ($j=$monthnumber;$j>=0;$j--){	
										$month_formated = sprintf("%02d",$j+1);
							   			if ($fromstatsmonth_sday=="$i-$month_formated"){$selected="selected";}else{$selected="";}
										echo "<OPTION value=\"$i-$month_formated\" $selected> $monthname[$j]-$i </option>";				
							   }
						}								
					?>										
					</select>
					</td></tr></table>
	  			</td>
    		</tr>	
			
			<tr>
				<td class="bar-search" align="left" bgcolor="#555577">			
					<font face="verdana" size="1" color="#ffffff"><b>&nbsp;&nbsp;Направление</b></font>
				</td>				
				<td class="bar-search" align="left" bgcolor="#cddeff">
				<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td>&nbsp;&nbsp;<INPUT TYPE="text" NAME="dst" value="<?php  echo $dst?>" class="intxt"></td>
				<td class="bar-search" align="center" bgcolor="#cddeff"><input type="radio" NAME="dsttype" value="1" <?php if((!isset($dsttype))||($dsttype==1)){?>checked<?php }?>>Точно</td>
				<td class="bar-search" align="center" bgcolor="#cddeff"><input type="radio" NAME="dsttype" value="2" <?php if($dsttype==2){?>checked<?php }?>>В начале</td>
				<td class="bar-search" align="center" bgcolor="#cddeff"><input type="radio" NAME="dsttype" value="3" <?php if($dsttype==3){?>checked<?php }?>>Содержит</td>
				<td class="bar-search" align="center" bgcolor="#cddeff"><input type="radio" NAME="dsttype" value="4" <?php if($dsttype==4){?>checked<?php }?>>В конце</td>
				</tr></table></td>
			</tr>			
			<tr>
				<td align="left" bgcolor="#000033">					
					<font face="verdana" size="1" color="#ffffff"><b>&nbsp;&nbsp;Источник</b></font>
				</td>				
				<td class="bar-search" align="left" bgcolor="#acbdee">
				<table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#acbdee"><tr><td>&nbsp;&nbsp;<INPUT TYPE="text" NAME="src" value="<?php echo "$src";?>" class="intxt"></td>
				<td class="bar-search" align="center" bgcolor="#acbdee"><input type="radio" NAME="sourcetype" value="1" <?php if((!isset($sourcetype))||($sourcetype==1)){?>checked<?php }?>>Точно</td>
				<td class="bar-search" align="center" bgcolor="#acbdee"><input type="radio" NAME="sourcetype" value="2" <?php if($sourcetype==2){?>checked<?php }?>>В начале</td>
				<td class="bar-search" align="center" bgcolor="#acbdee"><input type="radio" NAME="sourcetype" value="3" <?php if($sourcetype==3){?>checked<?php }?>>Содержит</td>
				<td class="bar-search" align="center" bgcolor="#acbdee"><input type="radio" NAME="sourcetype" value="4" <?php if($sourcetype==4){?>checked<?php }?>>В конце</td>
				</tr></table></td>
			</tr>
			<tr>
				<td class="bar-search" align="left" bgcolor="#555577">				
					<font face="verdana" size="1" color="#ffffff"><b>&nbsp;&nbsp;CallerID</b></font>
				</td>				
				<td class="bar-search" align="left" bgcolor="#cddeff">
				<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td>&nbsp;&nbsp;<INPUT TYPE="text" NAME="clid" value="<?php  echo $clid?>" class="intxt"></td>
				<td class="bar-search" align="center" bgcolor="#cddeff"><input type="radio" NAME="clidtype" value="1" <?php if((!isset($clidtype))||($clidtype==1)){?>checked<?php }?>>Точно</td>
				<td class="bar-search" align="center" bgcolor="#cddeff"><input type="radio" NAME="clidtype" value="2" <?php if($clidtype==2){?>checked<?php }?>>В начале</td>
				<td class="bar-search" align="center" bgcolor="#cddeff"><input type="radio" NAME="clidtype" value="3" <?php if($clidtype==3){?>checked<?php }?>>Содержит</td>
				<td class="bar-search" align="center" bgcolor="#cddeff"><input type="radio" NAME="clidtype" value="4" <?php if($clidtype==4){?>checked<?php }?>>В конце</td>
				</tr></table></td>
			</tr>
			<tr>
				<td align="left" bgcolor="#000033">					
					<font face="verdana" size="1" color="#ffffff"><b>&nbsp;&nbsp;Userfield</b></font>
				</td>				
				<td class="bar-search" align="left" bgcolor="#acbdee">
				<table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#acbdee"><tr><td>&nbsp;&nbsp;<INPUT TYPE="text" NAME="userfield" value="<?php echo "$userfield";?>" class="intxt"></td>
				<td class="bar-search" align="center" bgcolor="#acbdee"><input type="radio" NAME="userfieldtype" value="1" <?php if((!isset($userfieldtype))||($userfieldtype==1)){?>checked<?php }?>>Точно</td>
				<td class="bar-search" align="center" bgcolor="#acbdee"><input type="radio" NAME="userfieldtype" value="2" <?php if($userfieldtype==2){?>checked<?php }?>>В начале</td>
				<td class="bar-search" align="center" bgcolor="#acbdee"><input type="radio" NAME="userfieldtype" value="3" <?php if($userfieldtype==3){?>checked<?php }?>>Содержит</td>
				<td class="bar-search" align="center" bgcolor="#acbdee"><input type="radio" NAME="userfieldtype" value="4" <?php if($userfieldtype==4){?>checked<?php }?>>В конце</td>
				</tr></table></td>
			</tr>
			<tr>
				<td class="bar-search" align="left" bgcolor="#555577">				
					<font face="verdana" size="1" color="#ffffff"><b>&nbsp;&nbsp;Accountcode</b></font>
				</td>				
				<td class="bar-search" align="left" bgcolor="#cddeff">
				<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td>&nbsp;&nbsp;<INPUT TYPE="text" NAME="accountcode" value="<?php  echo $accountcode?>" class="intxt"></td>
				<td class="bar-search" align="center" bgcolor="#cddeff"><input type="radio" NAME="accountcodetype" value="1" <?php if((!isset($accountcodetype))||($accountcodetype==1)){?>checked<?php }?>>Точно</td>
				<td class="bar-search" align="center" bgcolor="#cddeff"><input type="radio" NAME="accountcodetype" value="2" <?php if($accountcodetype==2){?>checked<?php }?>>В начале</td>
				<td class="bar-search" align="center" bgcolor="#cddeff"><input type="radio" NAME="accountcodetype" value="3" <?php if($accountcodetype==3){?>checked<?php }?>>Содержит</td>
				<td class="bar-search" align="center" bgcolor="#cddeff"><input type="radio" NAME="accountcodetype" value="4" <?php if($accountcodetype==4){?>checked<?php }?>>В конце</td>
				</tr></table></td>
			</tr>			


			<tr>
        		<td class="bar-search" align="left" bgcolor="#000033"> </td>
						
				<td class="bar-search" align="center" bgcolor="#acbdee">
					<table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#acbdee"><tr><td>				
						<b>График :</b>
							<select name="type_gr" class="intxt">					
							<option value="calls" <?php  if (($type_gr=="calls") || !isset($type_gr)) { echo "selected";}?>>Звонки по часам</option>
							<option value="asr" <?php  if ($type_gr=="asr"){ echo "selected";}?>>ASR% по часам</option>
							<option value="callsasr" <?php  if ($type_gr=="callsasr"){ echo "selected";}?>>Звонки & ASR% по часам</option>
							</select>
						</td>
						<td align="right">							
							<input type="image"  name="image16" align="top" border="0" src="images/button-search.gif" />
							&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					</td></tr></table>
	  			</td>
						
    		</tr>
		</tbody></table>
	</FORM>
</center>





<!-- ** ** ** ** ** Part to display the GRAPHIC ** ** ** ** ** -->
<br><br>

<?php 
if (is_array($list) && count($list)>0){

$table_graph=array();
$table_graph_hours=array();
$numm=0;
foreach ($list_total as $recordset){
		$numm++;
		$mydate= substr($recordset[0],0,10);
		$mydate_hours= substr($recordset[0],0,13);
		//echo "$mydate<br>";
		if (is_array($table_graph_hours[$mydate_hours])){
			$table_graph_hours[$mydate_hours][0]++;
			$table_graph_hours[$mydate_hours][1]=$table_graph_hours[$mydate_hours][1]+$recordset[1];
		}else{
			$table_graph_hours[$mydate_hours][0]=1;
			$table_graph_hours[$mydate_hours][1]=$recordset[1];
		}
		
		
		if (is_array($table_graph[$mydate])){
			$table_graph[$mydate][0]++;
			$table_graph[$mydate][1]=$table_graph[$mydate][1]+$recordset[1];
		}else{
			$table_graph[$mydate][0]=1;
			$table_graph[$mydate][1]=$recordset[1];
		}
		if ($recordset[1]) {
		    $table_graph[$mydate][2]++;
		    $table_graph[$mydate][3]+=$recordset[2]-$recordset[1];
		}    		

}

$mmax=0;
$totalcall==0;
$totalminutes=0;
$totalcallsanswered=0;
$totalpdd=0;
foreach ($table_graph as $tkey => $data){	
	if ($mmax < $data[1]) $mmax=$data[1];
	$totalcall+=$data[0];
	$totalminutes+=$data[1];
	$totalcallsanswered+=$data[2];
	$totalpdd+=$data[3];
	
}

?>


<!-- TITLE GLOBAL -->
<center>
 <table border="0" cellspacing="0" cellpadding="0" width="80%"><tbody><tr><td align="left" height="30">
		<table cellspacing="0" cellpadding="1" bgcolor="#000000" width="50%"><tbody><tr><td>
			<table cellspacing="0" cellpadding="0" width="100%"><tbody>
				<tr><td bgcolor="#600101" align="left"><font face="verdana" size="1" color="white"><b>Всего</b></font></td></tr>
			</tbody></table>
		</td></tr></tbody></table>
 </td></tr></tbody></table>
		  
<!-- FIN TITLE GLOBAL MINUTES //-->
				
<table border="0" cellspacing="0" cellpadding="0" width="80%">
<tbody><tr><td bgcolor="#000000">			
	<table border="0" cellspacing="1" cellpadding="2" width="100%"><tbody>
	<tr>	
		<td align="center" bgcolor="#600101"></td>
    	<td bgcolor="#b72222" align="center" colspan="6"><font face="verdana" size="1" color="#ffffff"><b>Астериск Трафик</b></font></td>
    </tr>
	<tr bgcolor="#600101">
		<td align="left" bgcolor="#b72222"><font face="verdana" size="1" color="#ffffff"><b>Дата</b></font></td>
        <td align="center"><font face="verdana" size="1" color="#ffffff"><b>Длит-ть</b></font></td>
		<td align="center"><font face="verdana" size="1" color="#ffffff"><b>График</b></font></td>
		<td align="center"><font face="verdana" size="1" color="#ffffff"><b>Звонков</b></font></td>
		<td align="center"><font face="verdana" size="1" color="#ffffff"><b> <acronym title="Средняя продолжительность звонка, мин">ACD</acronym> </b></font></td>
		<td align="center"><font face="verdana" size="1" color="#ffffff"><b> <acronym title="Процент дозвона">ASR %</acronym> </b></font></td>
		<td align="center"><font face="verdana" size="1" color="#ffffff"><b> <acronym title="Среднее время соединения, сек">PDD</acronym> </b></font></td>
                			
		<!-- LOOP -->
	<?php  		
		$i=0;
		// #ffffff #cccccc
		foreach ($table_graph as $tkey => $data){	
		$i=($i+1)%2;		
		if ($data[2]) $tmc = $data[1]/$data[2];
		else $tmc=0;
		if ($data[0]) $asr = $data[2]/$data[0]*100;
		else $asr=0;
		if ($data[2]) $pdd = $data[3]/$data[2];
		else $pdd=0;
		
		$tmc_60 = sprintf("%02d",intval($tmc/60)).":".sprintf("%02d",intval($tmc%60));		
		
		$minutes_60 = sprintf("%02d",intval($data[1]/60)).":".sprintf("%02d",intval($data[1]%60));
		if ($mmax) $widthbar= intval(($data[1]/$mmax)*130);
		else $widthbar=0;
		
		//bgcolor="#336699" 
	?>
		</tr><tr>
		<td align="right" class="sidenav" nowrap="nowrap"><font face="verdana" size="1" color="#ffffff"><?php  echo $tkey?></font></td>
		<td bgcolor="<?php  echo $FG_TABLE_ALTERNATE_ROW_COLOR[$i]?>" align="right" nowrap="nowrap"><font face="verdana" color="#000000" size="1"><?php  echo $minutes_60?> </font></td>
        <td bgcolor="<?php  echo $FG_TABLE_ALTERNATE_ROW_COLOR[$i]?>" align="left" nowrap="nowrap" width="<?php  echo $widthbar+60?>">
        <table cellspacing="0" cellpadding="0"><tbody><tr>
        <td bgcolor="#e22424"><img src="images/spacer.gif" width="<?php  echo $widthbar?>" height="6"></td>
        </tr></tbody></table></td>
        <td bgcolor="<?php  echo $FG_TABLE_ALTERNATE_ROW_COLOR[$i]?>" align="right" nowrap="nowrap"><font face="verdana" color="#000000" size="1"><?php  echo $data[0]?></font></td>
        <td bgcolor="<?php  echo $FG_TABLE_ALTERNATE_ROW_COLOR[$i]?>" align="right" nowrap="nowrap"><font face="verdana" color="#000000" size="1"><?php  echo $tmc_60?> </font></td>
	<td bgcolor="<?php  echo $FG_TABLE_ALTERNATE_ROW_COLOR[$i]?>" align="right" nowrap="nowrap"><font face="verdana" color="#000000" size="1"><?php  echo number_format($asr,1)?> </font></td>
	<td bgcolor="<?php  echo $FG_TABLE_ALTERNATE_ROW_COLOR[$i]?>" align="right" nowrap="nowrap"><font face="verdana" color="#000000" size="1"><?php  echo number_format($pdd,1)?> </font></td>
     <?php 	 }	 
	 	if ($totalcallsanswered) $total_tmc_60 = sprintf("%02d",intval(($totalminutes/$totalcallsanswered)/60)).":".sprintf("%02d",intval(($totalminutes/$totalcallsanswered)%60));
		else $total_tmc_60=0;				
		$total_minutes_60 = sprintf("%02d",intval($totalminutes/60)).":".sprintf("%02d",intval($totalminutes%60));
	 
	 ?>                   	
	</tr>
	<!-- FIN DETAIL -->		
	
				
				<!-- FIN BOUCLE -->

	<!-- TOTAL -->
	<tr bgcolor="#600101">
		<td align="left" nowrap="nowrap"><font face="verdana" size="1" color="#ffffff"><b>Всего</b></font></td>
		<td align="right" nowrap="nowrap"><font face="verdana" size="1" color="#ffffff"><b><?php  echo $total_minutes_60?> </b></font></td>
		<td align="center" nowrap="nowrap"><font face="verdana" size="1" color="#ffffff"><b></b></font></td>
		<td align="right" nowrap="nowrap"><font face="verdana" size="1" color="#ffffff"><b><?php  echo $totalcall?></b></font></td>
		<td align="right" nowrap="nowrap"><font face="verdana" size="1" color="#ffffff"><b><?php  echo $total_tmc_60?></b></font></td>                        
		<td align="right" nowrap="nowrap"><font face="verdana" size="1" color="#ffffff"><b><?php  if ($totalcall) echo number_format(($totalcallsanswered/$totalcall*100),1);
												      else echo "0.0";?></b></font></td>                        
		<td align="right" nowrap="nowrap"><font face="verdana" size="1" color="#ffffff"><b><?php  if ($totalcallsanswered) echo number_format($totalpdd/$totalcallsanswered,1);
												      else echo "0.0";?></b></font></td>                        
	</tr>
	<!-- FIN TOTAL -->

	  </tbody></table>
	  <!-- Fin Tableau Global //-->

</td></tr></tbody></table>
	<br>

 	<IMG SRC="graph_statbar.php?min_call=<?php  echo $min_call?>&fromstatsday_sday=<?php  echo $fromstatsday_sday?>&days_compare=<?php  echo $days_compare?>&fromstatsmonth_sday=<?php  echo $fromstatsmonth_sday?>&dsttype=<?php  echo $dsttype?>&sourcetype=<?php  echo $sourcetype?>&clidtype=<?php  echo $clidtype?>&channel=<?php  echo $channel?>&resulttype=<?php  echo $resulttype?>&dst=<?php  echo $dst?>&src=<?php  echo $src?>&clid=<?php  echo $clid?>&userfieldtype=<?php  echo $userfieldtype?>&userfield=<?php  echo $userfield?>&accountcodetype=<?php  echo $accountcodetype?>&accountcode=<?php  echo $accountcode?>&type_gr=<?php  echo $type_gr?>" ALT="График статистики">


<!-- ** ** ** ** ** HOURLY LOAD ** ** ** ** ** -->
&nbsp;
<br/>
	<center>Выберите часовой интервал для подробного отчета

	<FORM METHOD=POST ACTION="graph_hourdetail.php?posted=<?php  echo $posted?>&min_call=<?php  echo $min_call?>&fromstatsday_sday=<?php  echo $fromstatsday_sday?>&days_compare=<?php  echo $days_compare?>&fromstatsmonth_sday=<?php  echo $fromstatsmonth_sday?>&dsttype=<?php  echo $dsttype?>&sourcetype=<?php  echo $sourcetype?>&clidtype=<?php  echo $clidtype?>&channel=<?php  echo $channel?>&resulttype=<?php  echo $resulttype?>&dst=<?php  echo $dst?>&src=<?php  echo $src?>&clid=<?php  echo $clid?>&userfieldtype=<?php  echo $userfieldtype?>&userfield=<?php  echo $userfield?>&accountcodetype=<?php  echo $accountcodetype?>&accountcode=<?php  echo $accountcode?>" target="superframe">		


	<!-- ** ** ** ** ** HOURLY LOAD ** ** ** ** ** -->
		<table class="bar-status" width="60%" border="0" cellspacing="1" cellpadding="2" align="center">
			<tbody>		
			<tr>
			<td align="left" bgcolor="#000033">					
					<font face="verdana" size="1" color="#ffffff"><b>&nbsp;&nbsp;Часовой интервал :</b></font>
				</td>				
				<td class="bar-search" align="center" bgcolor="#acbdee">
				<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td>&nbsp;&nbsp;
				<select name="hourinterval" class="intxt">
					<?php  
						for ($i=0;$i<=23;$i++){							
							echo '<option value="'.sprintf("%02d",$i)."\"> Интервал [".sprintf("%02d",$i).'h to '.sprintf("%02d",$i+1).'h] </option>';
						}
					?>					
					</select>
				
				</td>				
				</tr></table></td>
			</tr>

			<tr>
				<td align="left" bgcolor="#555577">					
					<font face="verdana" size="1" color="#ffffff"><b>&nbsp;&nbsp;Тип графика :</b></font>
				</td>				
				<td class="bar-search" align="center" bgcolor="#cddeff">
				<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td>&nbsp;&nbsp;
				<select name="typegraph" class="intxt">
					<option value="fluctuation"> &nbspГрафик  флуктуаций&nbsp&nbsp</option> 
					<option value="watch-call" selected> &nbspГрафик звонков</option>
				</select>
				
				</td>				
				</tr></table></td>
			</tr>

			<tr>
        		<td class="bar-search" align="left" bgcolor="#000033"> </td>

				<td class="bar-search" align="center" bgcolor="#acbdee">
					<input type="image"  name="image16" align="top" border="0" src="images/button-search.gif" />				

	  			</td>
    		</tr>
		</tbody></table>
	</FORM>
</center>
<br>
<center>
    <iframe name="superframe" src="graph_hourdetail.php?posted=<?php  echo $posted?>&min_call=<?php  echo $min_call?>&fromstatsday_sday=<?php  echo $fromstatsday_sday?>&days_compare=<?php  echo $days_compare?>&fromstatsmonth_sday=<?php  echo $fromstatsmonth_sday?>&dsttype=<?php  echo $dsttype?>&sourcetype=<?php  echo $sourcetype?>&clidtype=<?php  echo $clidtype?>&channel=<?php  echo $channel?>&resulttype=<?php  echo $resulttype?>&dst=<?php  echo $dst?>&src=<?php  echo $src?>&clid=<?php  echo $clid?>&userfieldtype=<?php  echo $userfieldtype?>&userfield=<?php  echo $userfield?>&accountcodetype=<?php  echo $accountcodetype?>&accountcode=<?php  echo $accountcode?>" BGCOLOR=white	width=770 height=800 marginWidth=0 marginHeight=0  frameBorder=0  scrolling=yes>

    </iframe>
</center>


</center>

<?php  }else{ ?>
	<center><h3>Нет звонков на условиях выборки.</h3></center>
<?php  } ?>




